package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.entity.SetmealDto;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: 宋H
 * @Date: 2022/9/6
 * @Description: com.itheima.service.impl
 * @version: 1.0
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐信息
     * @param setmealDto
     * @return
     */
    public void setmealDishSave(SetmealDto setmealDto) {

        //首先保存套餐信息
        this.save(setmealDto);
        //在保存套餐中的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());//通过查询出的套餐id，设置SetmealDish表的套餐id
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id删除套餐信息
     * @param ids
     */
    @Override
    public void setmealRemove(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids!=null,Setmeal::getId,ids);
        List<Setmeal> list = this.list(lqw);
        for (Setmeal setmeal : list) {
            if(setmeal!=null){
                this.removeById(setmeal);
            }else{
                throw new CustomException("未知错误，稍后重试");
            }
        }
    }

    /**
     * 修改套餐的售卖状态
     * @param status
     * @param ids
     */
    @Override
    public void setmealStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids!=null,Setmeal::getId,ids);
        List<Setmeal> setmealList = this.list(lqw);
        for (Setmeal setmeal : setmealList) {
            if(setmeal!=null){
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }
    }

    /**
     * 通过ID菜询套餐信息和对应的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealById(Long id) {
        //首先获取套餐信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();

        //对象拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        //根据套餐id，获取菜品口味信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id!=null,SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    public void setmealUpdate(SetmealDto setmealDto) {
        //首先修改套餐信息
        this.updateById(setmealDto);

        //修改套餐对应的菜品信息
        //首先删除原先套餐对应的菜品信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);
        //在获取页面的菜品id添加到setmealDish表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            if (setmealDishes!=null){
                item.setSetmealId(setmealDto.getId());
            }
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
